package com.client;

import com.grpc.HelloProto;
import com.grpc.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GrpcClient {

    public static void main(String[] args) {
        ManagedChannelBuilder<?> localhost = ManagedChannelBuilder.forAddress("localhost", 9000);
        ManagedChannel channel = localhost.usePlaintext().build();
        try {
//            获取存根，代理客户端发送请求。就像调用本地方法一样
            HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub = HelloServiceGrpc.newBlockingStub(channel);
            HelloProto.HelloRequest helloRequest = HelloProto.HelloRequest.newBuilder()
                    .setName("宋江")
                    .build();
//        rpc调用获取响应数据
            HelloProto.HelloResponse hello = helloServiceBlockingStub.hello(helloRequest);
            String result = hello.getResult();
            System.out.println("客户端获取到的数据：" + result);
        }catch (Exception e){
        }finally {
            channel.shutdown();
        }
    }
}
